﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Bill_Add(Bill model ,int ACID);


[OperationContract]
string Bill_Update(Bill model ,int ACID);


[OperationContract]
string BillBybilltypeid_Delete(int billtypeid,int ACID);


[OperationContract]
Bill BillBybilltypeid_Get(int billtypeid,int ACID );


[OperationContract]
List<Bill> Bill_Get(int ACID);



}
public partial class Service : IService
{
#region  添加Bill表数据
/// <summary>
/// 添加Bill
/// </summary>
/// <param name="model">Bill实体</param>
/// <returns>空或错误</returns>
public string Bill_Add(Bill model,int ACID)
{
if(model.billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(string.IsNullOrEmpty(model.demo))
{
return "『示例』不能为空！";
}
if(string.IsNullOrEmpty(model.Mode))
{
return "『格式』不能为空！";
}
if(string.IsNullOrEmpty(model.surcode))
{
return "『单据前缀』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『单据名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Bill.Count(p=>p.billtypeid==model.billtypeid)>0)
{
return "主键已存在！";
}
ctx.Bill.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Bill表数据事物用重构
/// <summary>
/// 添加Bill事物用重构
/// </summary>
/// <param name="model">Bill实体</param>
/// <returns>空或错误</returns>
private string Bill_Add(Bill model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(string.IsNullOrEmpty(model.demo))
{
return "『示例』不能为空！";
}
if(string.IsNullOrEmpty(model.Mode))
{
return "『格式』不能为空！";
}
if(string.IsNullOrEmpty(model.surcode))
{
return "『单据前缀』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『单据名称』不能为空！";
}
if(ctx.Bill.Count(p=>p.billtypeid==model.billtypeid)>0)
{
return "主键已存在！";
}
ctx.Bill.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Bill表数据
/// <summary>
/// 修改Bill
/// </summary>
/// <param name="model">Bill实体</param>
/// <returns>空或错误</returns>
public string Bill_Update(Bill model,int ACID)
{
if(model.billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(string.IsNullOrEmpty(model.demo))
{
return "『示例』不能为空！";
}
if(string.IsNullOrEmpty(model.Mode))
{
return "『格式』不能为空！";
}
if(string.IsNullOrEmpty(model.surcode))
{
return "『单据前缀』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『单据名称』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Bill.Count(p=>p.billtypeid==model.billtypeid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Bill Billmodel=ctx.Bill.Single(p => p.billtypeid==model.billtypeid);
if(model.billtypeid!=null )
{
Billmodel.billtypeid=model.billtypeid;
}
if(!string.IsNullOrEmpty(model.name))
{
Billmodel.name=model.name;
}
if(!string.IsNullOrEmpty(model.surcode))
{
Billmodel.surcode=model.surcode;
}
if(!string.IsNullOrEmpty(model.Mode))
{
Billmodel.Mode=model.Mode;
}
if(!string.IsNullOrEmpty(model.sepa))
{
Billmodel.sepa=model.sepa;
}
if(!string.IsNullOrEmpty(model.demo))
{
Billmodel.demo=model.demo;
}
if(!string.IsNullOrEmpty(model.tablename))
{
Billmodel.tablename=model.tablename;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Bill表Bybilltypeid数据
/// <summary>
/// 删除BillBybilltypeid
/// </summary>
/// <param name="billtypeid">单据类型ID</param>
/// <returns>空或错误</returns>
public string BillBybilltypeid_Delete(int billtypeid,int ACID)
{
if(billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Bill.Count(p=>p.billtypeid==billtypeid)!=1)
{
return null;
}
Bill model=ctx.Bill.Single(p => p.billtypeid== billtypeid);
ctx.Bill.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Bill表Bybilltypeid数据[事物用重构]
/// <summary>
/// 删除BillBybilltypeid[事物用重构]
/// </summary>
/// <param name="billtypeid">单据类型ID</param>
/// <returns>空或错误</returns>
private string BillBybilltypeid_Delete(int billtypeid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(billtypeid==null )
{
return "『单据类型ID』不能为空！";
}
if(ctx.Bill.Count(p=>p.billtypeid==billtypeid)!=1)
{
return null;
}
Bill model=ctx.Bill.Single(p => p.billtypeid== billtypeid);
ctx.Bill.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Bill表Bybilltypeid数据
/// <summary>
/// 获取BillBybilltypeid
/// </summary>
/// <param name="billtypeid">单据类型ID</param>
/// <returns>当前表</returns>
public Bill BillBybilltypeid_Get(int billtypeid,int ACID)
{
if(billtypeid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Bill.Count(p=>p.billtypeid==billtypeid)!=1)
{
return null;
}
return ctx.Bill.SingleOrDefault(p=>p.billtypeid==billtypeid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


public List<Bill> Bill_Get(int ACID)
{
    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
    {
        return ctx.Bill.ToList();
    }
}

#region  获取Bill表数据
/// <summary>
/// 获取Bill
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Bill></returns>
public List<Bill> Bill_Get(Expression<Func<Bill, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Bill.ToList();
}
 else 
{
return ctx.Bill.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Bill.Where(predicate).ToList();
}
 else 
{
return ctx.Bill.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion




#region  获取Bill表数据计数
/// <summary>
/// 获取Bill表数据计数
/// </summary>
/// <returns>List<Bill></returns>
public int Bill_GetCount(Expression<Func<Bill, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Bill.Count();

}
 else
{
return ctx.Bill.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Bill分页表数据
/// <summary>
/// 获取Bill分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Bill></returns>
public List<Bill> Bill_Getpage(int pageIndex, int pageSize,Expression<Func<Bill, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Bill.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Bill.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Bill.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Bill.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Bill表数据
/// <summary>
/// 批量修改Bill
/// </summary>
/// <param name="predicate">Expression<Func<Bill, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Bill,Bill>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Bill_batchUpdate(Expression<Func<Bill, bool>> predicate,Expression<Func<Bill, Bill>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Bill.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Bill表数据事物用重构
/// <summary>
/// 批量修改Bill事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Bill, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Bill,Bill>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Bill_batchUpdate(Expression<Func<Bill, bool>> predicate,Expression<Func<Bill, Bill>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Bill.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Bill表数据
/// <summary>
/// 批量删除Bill
/// </summary>
/// <param name="predicate">Expression<Func<Bill, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Bill_batchDel(Expression<Func<Bill, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Bill.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Bill表数据事物用重构
/// <summary>
/// 批量删除Bill事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Bill, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Bill_batchDel(Expression<Func<Bill, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Bill.Delete(predicate);
return ok;
}
#endregion


}
}

